package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.SubjectApi;
import com.yoda.mall.model.entity.cms.Subject;
import com.yoda.mall.model.entity.pms.Product;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.SubjectMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shawn
 * @date 2022年 12月 17日 11:37
 */
@DubboService
public class SubjectApiImpl implements SubjectApi {
    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public PageResult<Subject> selectByPage(Integer page, Integer pageSize, String keyword) {
        Page<Subject> productPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword),Subject::getTitle,keyword);

        wrapper.orderByDesc(Subject::getCreateTime);
        subjectMapper.selectPage(productPage, wrapper);
        return new PageResult<>(page, pageSize, (int) productPage.getTotal(), productPage.getRecords());
    }

    @Override
    public List<Subject> getAllSubject() {
        return subjectMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 获取全部商品专题
     * @return
     */
    @Override
    public List<Subject> subjectArr() {
        return subjectMapper.selectArr();
    }
}
